<script setup>
import { ref } from 'vue'
import Layout_top from '@/components/Layout_top.vue'
import Layout_foot from '@/components/Layout_foot.vue'

// 模拟订单数据
const orders = ref([
])
import { onMounted } from "vue";
import { getOrderListService, deleteOrderService } from "@/api/order.js";
onMounted(async () => {
    const res = await getOrderListService();
    orders.value = res.data;
})
import { ElMessageBox } from 'element-plus';
// 删除订单函数
const deleteOrder = async (orderId) => {
    ElMessageBox.confirm('确定删除？', '温馨提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        await deleteOrderService(orderId);
        orders.value = orders.value.filter(order => order.id !== orderId);
    });

}
</script>

<template>
    <div class="common-layout">
        <el-container>
            <el-header>
                <Layout_top></Layout_top>
            </el-header>
            <el-main>
                <div class="orders-container">
                    <h2>我的订单</h2>
                    <el-table :data="orders" stripe style="width: 100%">
                        <el-table-column prop="id" label="订单编号" width="180"></el-table-column>
                        <el-table-column prop="createdTime" label="下单日期" width="180">
                            <template #default="scope">
                                {{ new Date(scope.row.createdTime).toLocaleString() }}
                            </template>
                        </el-table-column>
                        <el-table-column label="商品信息">
                            <template #default="scope">
                                <ul>
                                    <li v-for="(item, index) in scope.row.orderItems" :key="index" class="product-item">
                                        <img :src="item.gpic" alt="Thumbnail" class="thumbnail">
                                        {{ item.gname }} x{{ item.quantity }} - ￥{{ item.price }}
                                    </li>
                                </ul>
                            </template>
                        </el-table-column>
                        <el-table-column prop="totalPrice" label="总价" width="180"></el-table-column>
                        <el-table-column label="操作" width="100">
                            <template #default="scope">
                                <el-button type="danger" size="small" @click="deleteOrder(scope.row.id)">删除</el-button>
                            </template>
                        </el-table-column>
                        <template #empty>
                            <div class="empty-cart">
                                <el-empty description="订单空空如也~" />
                            </div>
                        </template>
                    </el-table>
                </div>
            </el-main>
            <el-footer>
                <Layout_foot></Layout_foot>
            </el-footer>
        </el-container>
    </div>
</template>

<style lang="scss" scoped>
.common-layout {
    width: 100%;
    margin: auto;
}

.orders-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;

    h2 {
        text-align: center;
        margin-bottom: 20px;
        font-size: 24px;
        color: #333;
    }

    ul {
        list-style-type: none;
        padding: 0;

        .product-item {
            line-height: 1.8;
            color: #666;
            display: flex;
            align-items: center;

            .thumbnail {
                width: 50px; // 根据需要调整图片宽度
                height: 50px; // 根据需要调整图片高度
                object-fit: cover;
                margin-right: 10px;
            }
        }
    }
}
</style>

<style lang="scss" scoped>
.common-layout {
    width: 100%;
    margin: auto;
}

.orders-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;

    h2 {
        text-align: center;
        margin-bottom: 20px;
        font-size: 24px;
        color: #333;
    }

    ul {
        list-style-type: none;
        padding: 0;

        li {
            line-height: 1.8;
            color: #666;
        }
    }
}
</style>